import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { ApiService } from '@core/services/http/api.service';
import { GridConfigService } from 'app/core/config/grid-config.service';
import { GridOptions } from '@ag-grid-community/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { ICellRendererParams } from '@ag-grid-community/core';
import { Events } from '@core/services/events/Events';
import { ExportExcelService } from '@core/services/excel/export-excel.service';
import { ApesMessageService } from '@shared/apes/message';
import { ApesContextMenuService } from '@shared/ag-grid-ext/context-menu';
import { DatePipe } from '@angular/common';
import { UserAuthService } from '@core/services/user-auth.service';
import * as CryptoJS from 'crypto-js';
import { RenderOperatingButtonComponent } from '@shared/ag-grid-ext/cell-renders/render-operating-button.component';
import { dateFormat } from '../../../utils/utils';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig, FormlyFormOptions } from '@ngx-formly/core';
import { ApesDrawerCustomGridComponent } from '@shared/ag-grid-ext/drawer-custom/drawer-custom-grid.component';
import { ApesDrawerService } from '@shared/apes/drawer';

@Component({
  selector: 'waybill-particulars',
  templateUrl: './waybill-particulars.component.html',
  styleUrls: ['./waybill-particulars.component.scss'],
})
export class WaybillParticularsComponent implements OnInit {

  /* 按钮相关 */
  pageId: any = '';
  ctx = { model: {} };

  waybills: any[] = [];

  /** Grid 相关 **/
  parmes: any;
  rowData: any[] = [];

  /** formly 配置 **/
  form = new FormGroup({});
  formId = 'tms/waybill/waybillParticularsComponent';
  model: any = {};
  options: FormlyFormOptions = {};
  field: FormlyFieldConfig[] = [
    {
      key: 'WAYBILL_ID',
      type: 'span',
      templateOptions: {
        label: '运单',
      },
    },
    {
      key: 'CREATE_DATE',
      type: 'span',
      templateOptions: {
        label: '创建时间',
      },
    },
    {
      key: 'DELIVERY_ADDRESS',
      type: 'span',
      templateOptions: {
        label: '发货地址',
      },
    },
    {
      key: 'SHIPPING_ADDRESS',
      type: 'span',
      templateOptions: {
        label: '收货地址',
      },
    },
    {
      key: 'ESTIMATED_DELIVERY_TIME',
      type: 'span',
      templateOptions: {
        label: '预计送达',
      },
    },
    {
      key: 'LOADING_TIME',
      type: 'span',
      templateOptions: {
        label: '开始时间',
      },
    },
    {
      key: 'RECEIVED_DATE',
      type: 'span',
      templateOptions: {
        label: '结束时间',
      },
    },
    {
      key: 'CONSUME_TIME',
      type: 'span',
      templateOptions: {
        label: '耗时',
      },
    },
  ];

  selectAll: boolean = false;


  constructor(
    private apiService: ApiService,
    private event: Events,
    private routeInfo: ActivatedRoute,
    private gridConfigService: GridConfigService,
    protected drawerService: ApesDrawerService,
  ) {

  }

  ngOnInit() {

    this.event.subscribe(this.formId, () => {
      this.initData();
    });

    this.initData();

    // this.model = {
    //   waybillId: '000044',
    //   createDate: 'XXXX-XX-XX XX:XX:XX',
    //   address: {
    //     deliveryAddress: '广州沥滘店',
    //     shippingAddress: '佛山禅城店',
    //   },
    //   estimatedDeliveryTime: '2020年01月10日 15:00:00',
    //   startDate: '2020-01-16 10:00:05',
    //   receivedDate: '2020-01-16 17:35:49',
    //   hs: '7：35：44',
    // };

    // this.waybills = [
    //   {
    //     numbers: 'xxxxx',
    //     timeLine: [
    //       { text: '司机XXX2确认【佛山禅城店】货物签收已完成。2020-01-16 17:35:49' },
    //       { text: '【佛山禅城店】入库已完成。2020-01-16 17:27:29' },
    //       { text: '【佛山禅城店】收货入库中。2020-01-16 17:27:27' },
    //       { text: '司机XXX2已抵达【佛山禅城店】。2020-01-16 17:25:05' },
    //       { text: '【广州中转站】运单已生成。2020-01-16 15:00:05' },
    //     ],
    //   },
    //   {
    //     numbers: 'xxxxx',
    //     timeLine: [
    //       { text: '司机XXX2确认【佛山禅城店】货物签收已完成。2020-01-16 17:35:49' },
    //       { text: '【佛山禅城店】入库已完成。2020-01-16 17:27:29' },
    //       { text: '【佛山禅城店】收货入库中。2020-01-16 17:27:27' },
    //       { text: '司机XXX2已抵达【佛山禅城店】。2020-01-16 17:25:05' },
    //       { text: '【广州中转站】运单已生成。2020-01-16 15:00:05' },
    //     ],
    //   },
    // ];

  }

  ngAfterViewInit() {


  }

  initData() {
    let value = this.gridConfigService.getRouterData();

    if (value.hasOwnProperty('formId') && value['formId'] == this.formId) {
      let params = value.data;
      this.model = params;

      this.model['waybillId'] = params['WAYBILL_ID'];

      let api = params['api'];

      let data = { ...this.model };

      this.apiService.post(api, { 'body': JSON.stringify(data) }).subscribe((body) => {
        // console.log(body);
        if (body) {
          this.waybills = body['DATA'];
        }
      });
    }

  }

  checkParticulars() {
    let drawer;
    let api = 'tms.waybill.findByWaybill';
    const data = {
      node: this.model,
    };
    const openDrawer = drawer ? drawer : ApesDrawerCustomGridComponent;
    const drawerRef = this.drawerService.create<ApesDrawerCustomGridComponent, { formId: string, api: string, data: any }, string>({
      apesTitle: '货物详情信息',
      apesContent: openDrawer,
      apesWidth: '45vw',
      apesContentParams: {
        formId: api,
        api: api,
        data: data,
      },
    });
    drawerRef.afterOpen.subscribe(() => {

    });

    drawerRef.afterClose.subscribe(data => {
      if (typeof data === 'string') {

      }
    });
  }

}
